What is @babel/runtime?
The @babel/runtime package is a library that includes Babel modular runtime helpers and a version of regenerator-runtime. It is used to avoid polluting the global scope and to reduce the size of the compiled output by deduplicating helper functions that are commonly used by Babel-transpiled code. It is particularly useful when building libraries or applications that need to be optimized for size and scope isolation.
What are @babel/runtime's main functionalities?
Modularized Helpers
This feature allows you to use Babel's helper functions in a modular way, without including them in every file that needs them, thus reducing redundancy and file size.
import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
class MyClass {
constructor() {
_classCallCheck(this, MyClass);
// class body
}
}
Regenerator Runtime
Includes a version of the regenerator runtime to enable the use of async/await and generator functions in environments that do not natively support them.
import 'regenerator-runtime/runtime';
async function asyncCall() {
await someAsyncFunction();
}
Other packages similar to @babel/runtime
core-js
Similar to @babel/runtime, core-js is a modular standard library for JavaScript, which includes polyfills for ECMAScript features. It is often used in conjunction with Babel to ensure that newer language features will work in older environments. It is more comprehensive than @babel/runtime but can lead to larger bundle sizes if not used carefully.
tslib
tslib is a runtime library for TypeScript that includes helper functions similar to those in @babel/runtime. It is used to support features from TypeScript such as async/await, spread operators, and others without duplicating code. It is specific to TypeScript, whereas @babel/runtime is used with Babel and can be used with JavaScript or TypeScript.
@babel/runtime
babel's modular runtime helpers
See our website @babel/runtime for more information.
Install
Using npm:
npm install --save @babel/runtime
or using yarn:
yarn add @babel/runtime